<template>
	<view class="page">
		<view class="item">
			<text class="title">机构名称</text>
			<view class="content">
				<input type="text" v-model="institution" value="请输入机构的名称" placeholder="请输入机构的名称" />
			</view>
		</view>
		<view class="item">
			<text class="title">手机号</text>
			<view class="content">
				<input type="number" v-model="phone" placeholder="请输入您的手机号" maxlength="11" />
			</view>
		</view>
		<view class="item">
			<text class="title">机构地址</text>
			<view class="content">
				<input type="text" v-model="address" placeholder="请输入机构的地址" @click="chooselocation()" />
			</view>
		</view>
		<view class="item">
			<text class="title">上传图片</text>
			<view class="content">
				<input type="text" v-model="file" placeholder="请上传图片" @click="uploadImage()" />
			</view>
		</view>
		<view class="item">
			<text class="title">机构简介</text>
			<view class="content">
				<input type="text" v-model="insIntroduction" value="请输入机构的介绍" placeholder="请输入机构的介绍" />
			</view>
		</view>
		<view>

			<button class="sbutton" type="default" hover-class="button-hover" @click="submit()">保存</button>
		</view>
	</view>
</template>

<script>
	import cloudbase from '@cloudbase/js-sdk';
	
	const db = cloudbase.database();
	export default {
		data() {
			return {
				_id: '',
				_openid: '',
				institution: '',
				institutionId: getApp().globalData.userInfo.institutionId,
				address: getApp().globalData.userInfo.address,
				location: getApp().globalData.userInfo.location,
				insIntroduction: '',
				phone: '',
				file: '',
				tempFilePaths: [],
				tempFiles: [],
				fileList: '',
				bufferList: []
			}
		},
		created() {
			this.getInsInformation();
		},
		methods: {
			getInsInformation: function() {
				cloudbase.database().collection('institution').where({
					_id: getApp().globalData.userInfo.institutionId
				}).get().then(res => {
					console.log(res);
					let list = res.data;
					if (list.length != 0) {
						this.address = list[0].address;
						this.institution = list[0].institution;
						this.insIntroduction = list[0].insIntroduction;
						this.phone = list[0].phone;
						this.location = list[0].location;
						this._openid = list[0]._openid;
						this._id = list[0]._id;
					}
				})
			},

			chooselocation: function() {
				let that = this;
				uni.chooseLocation({
					success(res) {
						that.location = new db.Geo.Point(res.longitude, res.latitude);
						console.log(that.location);
						that.address = res.address;
					}
				})
			},
			uploadImage: function() {
				let that = this;
				that.bufferList = [];
				uni.chooseImage({
					count: 9,
					sizeType: ['original', 'compressed'],
					sourceType: ['album'],
					success(res) {
						console.log('uploadImage:')
						console.log(res);
						that.tempFilePaths = res.tempFilePaths;
						that.fileList = [];

						for (let key in that.tempFilePaths) {

							that.setFileList(key, res.tempFilePaths.length);

						}
					}
				})
			},
			setFileList: function(key, length) {
				console.log(key);
				console.log(length);
				let that = this;
				let _key = parseInt(key);
				let _length = length;
				console.log(_key, _length)
				// console.log({
				// 	cloudPath: "institutionImage/" + that.institutionId + "/" + key +
				// 		".png",
				// 	filePath: that.tempFilePaths[key],
				// 	onUploadProgress: function(progressEvent) {
				// 		let percentCompleted = Math.round((progressEvent.loaded *
				// 			100) / progressEvent.total);
				// 	}
				// })
				let str = that.randomString(3);
				cloudbase.uploadFile({
					cloudPath: "institutionImage/" + that.institutionId + "/" + str +
						".png",
					filePath: that.tempFilePaths[key],
					onUploadProgress: function(progressEvent) {
						let percentCompleted = Math.round((progressEvent.loaded *
							100) / progressEvent.total);
					}
				}).then(result => {
					// console.log(result)
					let i = new Object();

					i = result.fileID;
					that.bufferList.push(i);
					if (_key === _length - 1) {
						// console.log('ok')
						that.updateFile(that.bufferList);
					}
				})
				// return bufferList;
			},
			updateFile: function(fileList) {
				console.log(fileList);
				cloudbase.database().collection('institution').where({
					_openid: this._openid
				}).update({
					fileList: fileList
				}).then(res => {
					console.log(res);
					if (res.updated == 1) {
						uni.showToast({
							title: '上传成功'
						})
					}
				})
			},

			submit: function() {
				if (this.institution == '' || this.phone == '' || this.address == '' || this.insIntroduction == '') {
					uni.showToast({
						title: '请将信息填写完毕',
						icon: 'none'
					})
				} else {
					cloudbase.database().collection('institution').where({
						_id: this.institutionId
					}).update({
						institution: this.institution,
						phone: this.phone,
						address: this.address,
						location: this.location,
						insIntroduction: this.insIntroduction
					}).then(res => {
						console.log(res);
						if (res.updated == 1) {
							uni.showToast({
								title: '保存成功'
							})
							uni.navigateBack({
								delta: 1
							})
							// uni.navigateTo({
							// 	url: "../login/login?from=register"
							// })
						}
					})


				}
			},
			randomString: function(e) {
				e = e || 32;
				let t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
				let a = t.length;
				let n = "";
				for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
				return n
			}
		}
	}
</script>

<style>
	.page {
		width: 750rpx;
		height: 1334rpx;
		/* height: 1020rpx; */
		/* padding-bottom: 60px; */
		background-color: #F3F5F7;
	}

	.item {
		width: 622rpx;
		height: 133rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 20rpx;

	}

	.title {
		font-size: 26rpx;
		color: #666666;
		margin-bottom: 15rpx;

		/* margin-left: 64rpx; */
		/* margin-right: 64rpx; */

	}

	.content {
		width: 622rpx;
		height: 92rpx;
		background-color: #FFFFFF;
		box-shadow: 0px 4px 6px rgba(0, 0, 0, .05);
		border-radius: 16rpx;
		/* margin-left: 64rpx; */
		/* margin-right: 64rpx; */
		line-height: 92rpx;
		font-size: 14px;
		padding-left: 14px;
		color: #A2A2A2;
	}

	.content input {
		height: 92rpx;
		line-height: 92rpx;

		color: #A2A2A2;
	}

	.sbutton {
		text-align: center;
		margin-left: auto;
		margin-right: auto;
		width: 200rpx;
		height: 96rpx;
		background-color: #FD973F !important;
		border-radius: 20px;
		color: #FFFFFF !important;
		margin-top: 68rpx;
	}

	.button-hover {
		background-color: rgba(0, 0, 0, 0.1) !important;
		opacity: 0.7;
	}
</style>
